﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace pTools
{
    public partial class printWin : DevExpress.XtraEditors.XtraForm
    {
        cMethods.DBHelper ado;
        public printWin()
        {
            InitializeComponent();
            ado = new cMethods.DBHelper();
        }
        private VBprinter.EasyReport easyPrint;
  

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this.Text = "打印插件";
                PrintDocument();
                LoadPrintTemp();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 打印机列表
        /// </summary>
        private void PrintDocument()
        {
            PrintDocument fPrintDocument = new PrintDocument();
            string defaultPrinter = fPrintDocument.PrinterSettings.PrinterName;
            for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++)
            {
                string tmp = PrinterSettings.InstalledPrinters[i];
                cbPrint.Items.Add(tmp);
            }
            cbPrint.Text = defaultPrinter;
        }
        /// <summary>
        /// 打印模板
        /// </summary>
        private void LoadPrintTemp()
        {
            string sql = "select * from bar_PrintTemplates_list where status=1";
            this.cbTemplate.DataSource = ado.getData(sql, "");
            cbTemplate.DisplayMember = "lbFile";
        }
        private void btnClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnPrint_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                string sql = "select * from bar_printData_info where status=1 order by id desc";
                var table = ado.getData(sql,"");
                if (table.Rows.Count > 0)
                {
                    var row = table.Rows[0];
                    if (row["target"].ToString() == "barPrint")
                    {
                        this.printBar(row["doSql"].ToString());
                    }
                    sql = "update bar_printData_info set status=0 where id="+table.Rows[0]["id"];
                    ado.UpdateData(sql);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="barCodes"></param>
        private void printBar(string barCodes)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from bar_ProductBarCode_info where ");
            sb.Append("barcode in(" + barCodes + ")");
            var table = ado.getData(sb.ToString(),"");
            this.DoPrint(table);
        }
        /// <summary>
        /// 打印方法
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private bool DoPrint(DataTable dt)
        {
            easyPrint = new VBprinter.EasyReport();
            string currentDir = AppDomain.CurrentDomain.BaseDirectory;
            string configFile = Path.Combine(currentDir + "Template",this.cbTemplate.Text);            //路径拼接
            if (File.Exists(configFile))
            {
                easyPrint.SetReportDataSource(dt);
                easyPrint.SetReportFile(configFile);
                easyPrint.PrintReport(false, true, false, false, cbPrint.Text).ToString();
                return true;
            }
            else
            {
                MessageBox.Show("打印模板不存在！","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);      //未选择模版
                return false;
            }
        }
    }
}
